package com.example.goodog;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;


/**
 * @Author Paresh N. Mayani
 * @Web http://www.technotalkative.com
 */


public class Utils {
	public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
	
	/*
	 * Below function is used to Convert an INPUTSTREAM into the STRING
	 */
	public static String iStream_to_String(InputStream is1)
	{
			 BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
			 String line;
			 StringBuilder sb =  new StringBuilder();
			 try {
				while ((line = rd.readLine()) != null) {
				 		sb.append(line);
				 }
				 rd.close();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 String contentOfMyInputStream = sb.toString();
			 return contentOfMyInputStream;
	}
	
	
	public static Address LatLngToAddress(Context context, double latitude, double longitude)
	{
		Geocoder geocoder;
		List<Address> addresses;
		geocoder = new Geocoder(context, Locale.getDefault());
		try {
			addresses = geocoder.getFromLocation(latitude, longitude, 1);
			if(addresses!=null && addresses.size()>0)
			{
				return addresses.get(0);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String AdressToString(Address address)
	{
		if(address == null)
		{
			return "";
		}
		StringBuilder LatLong = new StringBuilder("");
		
		for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
		{
			LatLong.append(address.getAddressLine(i)).append(", ");
		}
		return LatLong.toString();
	}
	
	public static String DateToString(Date date, String format)
	{
		if(date == null)
		{
			return "";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		
		return sdf.format(date);
	}
	
	public static Date StringToDate(String str, String format)
	{
		if(str == "")
		{
			return null;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static int monthsBetween(Date minuend, Date subtrahend)  
	{  
		
		Calendar cal = Calendar.getInstance();  
		// default will be Gregorian in US Locales  
		cal.setTime(minuend);  
		int minuendMonth =  cal.get(Calendar.MONTH)+1;  
		Log.i("gg", "minuendMonth "+minuendMonth);
		int minuendYear = cal.get(Calendar.YEAR);  
		Log.i("gg", "minuendYear "+minuendYear);
		cal.setTime(subtrahend);  
		int subtrahendMonth =  cal.get(Calendar.MONTH)+1;  
		Log.i("gg", "subtrahendMonth "+subtrahendMonth);
		int subtrahendYear = cal.get(Calendar.YEAR); 
		Log.i("gg", "subtrahendYear "+subtrahendYear);

		// the following will work okay for Gregorian but will not  
		// work correctly in a Calendar where the number of months   
		// in a year is not constant  
		return ((minuendYear - subtrahendYear) * 12) +    
				(minuendMonth - subtrahendMonth);  
	} 
	
	public static int DateToAgeInMonthes(Date date)  
	{  
		Date now = new Date();
	    return Utils.monthsBetween(now,date);
	} 
	
	


	
	
	
}